<?php
/*
 * Copyright (C) 2010 Emanuele Colonnelli <emanuele.colonnelli@phpstruct.org>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

namespace org\phpstruct\core;

/**
 * Description of Logger
 *
 * @author Emanuele Colonnelli <emanuele.colonnelli@phpstruct.org>
 */
class Logger {
    const TYPE_INFO = 'INFO';
    const TYPE_ERROR = 'ERROR';
    const TYPE_WARNING = 'WARNING';
    const TYPE_FATAL = 'FATAL';
    const TYPE_DEBUG = 'DEBUG';

    public static function log($message=null, $type=self::TYPE_INFO) {

        $time = date("Ymd");

        $path = sprintf('%s%slog%serror%s.log',$_SERVER['DOCUMENT_ROOT'],\DIRECTORY_SEPARATOR,\DIRECTORY_SEPARATOR,date('Ymd'));

        error_log("===========================================\n\n", 3, $path);


        if (is_string($message)) {
            error_log(sprintf('[%s] - %s -', $type, $time, $message ? $message : "NO MESSAGE"), 3, $path);
        } elseif ($message instanceof Exception) {
            $message = new Exception();

            error_log(sprintf('[%s] - %s -', 'EXCEPTION', $time, $message->getMessage()), 3, $path);
            error_log($message->getTraceAsString(), 3, $path);
        } else {
            error_log(sprintf('[%s] - %s -', $type, $time, $message ? $message : "NO MESSAGE"), 3, $path);
        }

        if (isset($_REQUEST)) {
            error_log("REQUEST VARS:\n\n", 3, $path);
            error_log(print_r($_REQUEST, 1), 3, $path);
        }

        error_log("===========================================\n\n", 3, $path);
    }


}

